﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;

namespace BO
{
    public class DescriptionHandler
    {
        public static readonly Assembly Asm = Assembly.GetExecutingAssembly();

        public static List<KeyValuePair<string, string>> GetAllFields(string tableName)
        {
            var list = new List<KeyValuePair<string, string>>();
            
            var t = Asm.GetType(GlobalVar.BOAssembly + "." + tableName);
            foreach (var p in t.GetProperties())
            {
                FieldDescriptionAttribute[] attr = (FieldDescriptionAttribute[])p.GetCustomAttributes(typeof(FieldDescriptionAttribute), false);
                if (attr != null && attr.Length > 0)
                {
                   var description = attr[0].Description;
                    list.Add(new KeyValuePair<string,string>(p.Name,description));
                }
            }
            return list;
        }

        public static List<KeyValuePair<string, string>> GetAllTables()
        {
            var list = new List<KeyValuePair<string, string>>();

            var types = Asm.GetTypes();
            foreach (var t in types)
            {
                if (t.BaseType == typeof(BusinessBaseEx)){
                    var d=DescriptionHandler.GetTableDescription(t.Name);
                    if(d==t.Name)
                        continue;
                    list.Add(new KeyValuePair<string, string>(t.Name, d));
                }
            }
            return list;
        }

        public static string GetTableDescription(string tableName)
        {
            var description = tableName;
            var t = Asm.GetType(GlobalVar.BOAssembly + "." + tableName);
            FieldDescriptionAttribute[] attr = (FieldDescriptionAttribute[])t.GetCustomAttributes(typeof(FieldDescriptionAttribute), false);
            if (attr != null && attr.Length > 0)
            {
                description = attr[0].Description;
            }

            return description;
        }

        public static string GetDescription(string tableName, string fieldName)
        {
            var description = fieldName;

            var t = Asm.GetType(GlobalVar.BOAssembly + "." + tableName);
            var p = t.GetProperty(fieldName);
            FieldDescriptionAttribute[] attr = (FieldDescriptionAttribute[])p.GetCustomAttributes(typeof(FieldDescriptionAttribute), false);
            if (attr != null && attr.Length > 0)
            {
                description = attr[0].Description;
            }

            return description;
        }
        public static string GetDescription(Enum enumValue)
        {
            if (enumValue == null) throw new ArgumentNullException("value");
            string description = enumValue.ToString();
            FieldInfo fieldInfo = enumValue.GetType().GetField(description);
            FieldDescriptionAttribute[] attr = (FieldDescriptionAttribute[])fieldInfo.GetCustomAttributes(typeof(FieldDescriptionAttribute), false);
            if (attr != null && attr.Length > 0)
            {
                description = attr[0].Description;
            }

            return description;
        }

        public static IList ToList(Type type)
        {
            if (type == null) throw new ArgumentNullException("type");
            if (!type.IsEnum) throw new ArgumentException("Type provided must be an Enum.", "type");

            ArrayList list = new ArrayList();
            Array array = Enum.GetValues(type);
            foreach (Enum value in array)
            {
                list.Add(new KeyValuePair<Enum, string>(value, GetDescription(value)));
            }

            return list;
        }
    }

    [AttributeUsage(AttributeTargets.All, AllowMultiple = false)]
    public class FieldDescriptionAttribute : Attribute
    {
        private string description;

        public string Description
        {
            get
            {
                return this.description;
            }
        }

        public FieldDescriptionAttribute(string Description = "")
            : base()
        {
            this.description = Description;
        }
    }
}
